{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分支结构和循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分支结构 (条件语句）\n",
    "\n",
    "1. 单项分支\n",
    "2. 双项分支\n",
    "3. 多项分支\n",
    "3. 巢状分支"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**流程：**  计算机执行代码的顺序\n",
    "\n",
    "**流程控制：** 对计算机代码执行顺序的管理\n",
    "\n",
    "**流程控制分类：** \n",
    "\n",
    "    1. 顺序结构\n",
    "    2. 分支结构/选择结构, i.e. 路口选择\n",
    "    3. 循环结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  单项分支\n",
    "\n",
    "if condition：\n",
    "\n",
    "    code\n",
    "    code \n",
    "   \n",
    "if 条件表达式如果为真，则执行if后所控制的代码组（后面的N行有相同的缩进），如果为假，则不执行\n",
    "\n",
    "：之后的语句必须缩进，否则语法错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start\n",
      "选择性语句的执行\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "print('start')\n",
    "if True:\n",
    "    print('选择性语句的执行')\n",
    "print('end')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start\n",
      "end\n"
     ]
    }
   ],
   "source": [
    "# change condition\n",
    "print('start')\n",
    "if 10>100:\n",
    "    print('选择性语句的执行')\n",
    "print('end')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  双项分支\n",
    "\n",
    "if condition：\n",
    "\n",
    "    code\n",
    "    code \n",
    "    \n",
    "else:\n",
    "    \n",
    "    code\n",
    "    code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入成绩：70\n",
      "及格\n"
     ]
    }
   ],
   "source": [
    "# 成绩分段判断\n",
    "grade = int(input('输入成绩：'))\n",
    "\n",
    "if grade >= 60:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  多项分支\n",
    "\n",
    "if condition：\n",
    "\n",
    "    code\n",
    "    code \n",
    "    \n",
    "elif condition:\n",
    "    \n",
    "    code\n",
    "    code\n",
    "    \n",
    "elif condition:\n",
    "\n",
    "    code\n",
    "    code\n",
    "    \n",
    "else:\n",
    "\n",
    "多项分支可以添加无限个elif分支，无论如何只会执行一个分支\n",
    "\n",
    "执行完一个分支后，分支结构就会结束，后面的分支都不会判断也不会执行\n",
    "\n",
    "多项分支的判断顺序是自上而下逐个分支判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入成绩：100\n",
      "优秀\n"
     ]
    }
   ],
   "source": [
    "# 多分支成绩判断\n",
    "\n",
    "grade = int(input('输入成绩：'))\n",
    "\n",
    "if grade >= 85:\n",
    "    print('优秀')\n",
    "elif grade >=70:\n",
    "    print('良好')\n",
    "elif grade >=60:\n",
    "    print('及格')\n",
    "else:\n",
    "    print('不及格')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入成绩：80\n",
      "jige\n"
     ]
    }
   ],
   "source": [
    "# 一本书中的例子\n",
    "grade = int(input('输入成绩：'))\n",
    "\n",
    "if grade >= 60:\n",
    "    print('jige')\n",
    "elif grade >=70:\n",
    "    print('良好')\n",
    "elif grade >=85:\n",
    "    print('youxiu')\n",
    "else:\n",
    "    print('不及格')\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 巢状分支\n",
    "\n",
    "其他分支结构的嵌套结构，无论哪个分支都可以嵌套\n",
    "\n",
    "```\n",
    "if condition:\n",
    "\n",
    "    if condition:\n",
    "        code\n",
    "        code\n",
    "    else:\n",
    "        code\n",
    "        code\n",
    "\n",
    "else:\n",
    "\n",
    "    if condition:\n",
    "        code\n",
    "        code\n",
    "    else:\n",
    "        code\n",
    "        code\n",
    "```   \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入成绩：90\n",
      "优秀\n"
     ]
    }
   ],
   "source": [
    "# 成绩判断\n",
    "\n",
    "grade = int(input('输入成绩：'))\n",
    "\n",
    "if grade >= 70:\n",
    "    if grade>=85:\n",
    "        print('优秀')\n",
    "    else:\n",
    "        print('良好')\n",
    "else:\n",
    "    if grade >=60:\n",
    "        print('及格')\n",
    "    else:\n",
    "        print('不及格')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 循环结构"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### while\n",
    "\n",
    "while condition:\n",
    "\n",
    "    code \n",
    "    [变量的变化]\n",
    "    \n",
    "while condition:\n",
    "    \n",
    "    code\n",
    "    [变量的变化]\n",
    "    \n",
    "else:\n",
    "\n",
    "    code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1   2   3   4   5   6   7   8   9   10   "
     ]
    }
   ],
   "source": [
    "# 循环输出1-10的值\n",
    "\n",
    "i=1\n",
    "\n",
    "while i<=10:\n",
    "    print(i,end='   ')\n",
    "    i += 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10   9   8   7   6   5   4   3   2   1   "
     ]
    }
   ],
   "source": [
    "# 循环输出10-1的值\n",
    "\n",
    "i=10\n",
    "\n",
    "while i>=1:\n",
    "    print(i,end='   ')\n",
    "    i -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:53:39.104096Z",
     "start_time": "2022-03-10T03:53:39.083153Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum: 5050\n"
     ]
    }
   ],
   "source": [
    "# 计算1-100的累加\n",
    "\n",
    "i=1\n",
    "total = 0\n",
    "while i<=100:\n",
    "    total += i\n",
    "    i += 1\n",
    "   \n",
    "print('sum:',total)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个值：e\n",
      "内容： e\n",
      "请输入一个值：5\n",
      "内容： 5\n",
      "请输入一个值：6\n",
      "内容： 6\n",
      "请输入一个值：q\n",
      "内容： q\n"
     ]
    }
   ],
   "source": [
    "# 死循环的使用\n",
    "\n",
    "while True:\n",
    "    k=input(\"请输入一个值：\")\n",
    "    print('内容：',k)\n",
    "    if k=='q':\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  for...in \n",
    "\n",
    "用于遍历容器类的数据 （str,List, Tuple, Set, Dict）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "j; i; j; i; j; i; j; i; "
     ]
    }
   ],
   "source": [
    "# 遍历字符串\n",
    "s = 'jijijiji'\n",
    "for i in s:\n",
    "    print(i,end='; ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1; 2; 3; 4; 6; "
     ]
    }
   ],
   "source": [
    "# 遍历列表\n",
    "a=[1,2,3,4,6]\n",
    "for i in a:\n",
    "    print(i,end='; ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:54:48.782953Z",
     "start_time": "2022-03-10T03:54:48.763006Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('age', 20), ('name', 'April')])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 遍历Dict\n",
    "a={'age':20, 'name':'April'}\n",
    "# for i in a.values():\n",
    "#     print(i)\n",
    "\n",
    "a.items()\n",
    "\n",
    "#a=[1,2,3,4,30,5,70,40]\n",
    "#a.sort(reverse=True)\n",
    "#a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:54:56.424641Z",
     "start_time": "2022-03-10T03:54:56.418656Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age 20\n",
      "name April\n"
     ]
    }
   ],
   "source": [
    "# dict.items()\n",
    "for (key,value) in a.items():\n",
    "    print(key,value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:55:31.282291Z",
     "start_time": "2022-03-10T03:55:31.261348Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{20: 'age', 'April': 'name'}\n"
     ]
    }
   ],
   "source": [
    "# dict, k,v 换地方\n",
    "a={'age':20, 'name':'April'}\n",
    "new_dict = {}\n",
    "\n",
    "for key, value in a.items():\n",
    "    new_dict[value] = key\n",
    "    \n",
    "print(new_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:56:58.370049Z",
     "start_time": "2022-03-10T03:56:58.361072Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n",
      "2 3\n",
      "4 3\n"
     ]
    }
   ],
   "source": [
    "# 遍历嵌套数据结构\n",
    "a=((1,2),(2,3),(4,3))\n",
    "for i,j in a:\n",
    "    print(i,j)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T03:58:10.906875Z",
     "start_time": "2022-03-10T03:58:10.896873Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 2, 3, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "b = []\n",
    "for i in a:\n",
    "    for  j in i:\n",
    "        b.append(j)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### range()\n",
    "\n",
    "遍历数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0; 1; 2; 3; 4; 5; 6; 7; 8; 9; "
     ]
    }
   ],
   "source": [
    "# 循环输出0-9\n",
    "for i in range(0,10):\n",
    "    print(i,end='; ')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2; 3; 4; 5; 6; "
     ]
    }
   ],
   "source": [
    "# 循环输出2-6\n",
    "for i in range(2,7):\n",
    "    print(i,end='; ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 5 10 15 20 25 30 35 40 45 50 55 60 "
     ]
    }
   ],
   "source": [
    "# 循环输出0-60 递增5 \n",
    "for i in range(0,61,5):\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 9 8 7 6 5 4 3 2 1 "
     ]
    }
   ],
   "source": [
    "# 递减：输出10-1的值\n",
    "for i in range(10,0,-1):\n",
    "    print(i,end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1  2  3  4  5  "
     ]
    }
   ],
   "source": [
    "# 使用 range()  and len() 结合遍历输出List里的内容\n",
    "\n",
    "a=[1,2,3,4,5]\n",
    "for i in range(0,len(a)):\n",
    "    print(a[i],end='  ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T04:01:14.174616Z",
     "start_time": "2022-03-10T04:01:14.160651Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "round 0\n",
      "0 1\n",
      "round 1\n",
      "1 2\n",
      "round 2\n",
      "2 3\n",
      "round 3\n",
      "3 4\n",
      "round 4\n",
      "4 5\n"
     ]
    }
   ],
   "source": [
    "#enumerate(a)\n",
    "a=[1,2,3,4,5]\n",
    "for i,v in enumerate(a):\n",
    "    print('round %d' % i)\n",
    "    print(i,v)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1   1*2=2   1*3=3   1*4=4   1*5=5   1*6=6   1*7=7   1*8=8   1*9=9   \n",
      "2*1=2   2*2=4   2*3=6   2*4=8   2*5=10  2*6=12  2*7=14  2*8=16  2*9=18  \n",
      "3*1=3   3*2=6   3*3=9   3*4=12  3*5=15  3*6=18  3*7=21  3*8=24  3*9=27  \n",
      "4*1=4   4*2=8   4*3=12  4*4=16  4*5=20  4*6=24  4*7=28  4*8=32  4*9=36  \n",
      "5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  5*6=30  5*7=35  5*8=40  5*9=45  \n",
      "6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  6*7=42  6*8=48  6*9=54  \n",
      "7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  7*8=56  7*9=63  \n",
      "8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  8*9=72  \n",
      "9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  \n"
     ]
    }
   ],
   "source": [
    "# Exercise: 9*9乘法表\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-10T04:04:19.131170Z",
     "start_time": "2022-03-10T04:04:19.105234Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1   1*2=2   1*3=3   1*4=4   1*5=5   1*6=6   1*7=7   1*8=8   1*9=9   \n",
      "2*1=2   2*2=4   2*3=6   2*4=8   2*5=10  2*6=12  2*7=14  2*8=16  2*9=18  \n",
      "3*1=3   3*2=6   3*3=9   3*4=12  3*5=15  3*6=18  3*7=21  3*8=24  3*9=27  \n",
      "4*1=4   4*2=8   4*3=12  4*4=16  4*5=20  4*6=24  4*7=28  4*8=32  4*9=36  \n",
      "5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  5*6=30  5*7=35  5*8=40  5*9=45  \n",
      "6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  6*7=42  6*8=48  6*9=54  \n",
      "7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  7*8=56  7*9=63  \n",
      "8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  8*9=72  \n",
      "9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  \n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,10):\n",
    "        m = i*j\n",
    "        print('{}*{}={:<2}'.format(i,j,m), end='  ')\n",
    "        #print(str(i)+'*'+str(j)+'='+str(m), end=' ')\n",
    "    print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1  \n",
      "2*1=2  2*2=4  \n",
      "3*1=3  3*2=6  3*3=9  \n",
      "4*1=4  4*2=8  4*3=12 4*4=16 \n",
      "5*1=5  5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n"
     ]
    }
   ],
   "source": [
    "# Exercise: 9*9乘法表\n",
    "# 调整成为三角形\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1   \n",
      "2*1=2   2*2=4   \n",
      "3*1=3   3*2=6   3*3=9   \n",
      "4*1=4   4*2=8   4*3=12  4*4=16  \n",
      "5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  \n",
      "6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  \n",
      "7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  \n",
      "8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  \n",
      "9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  \n"
     ]
    }
   ],
   "source": [
    "for row in range(1,10):\n",
    "    for col in range(1,row+1):\n",
    "        m = row*col\n",
    "        print('{}*{}={:<2}'.format(row,col,m), end='  ')\n",
    "        #print(str(i)+'*'+str(j)+'='+str(m), end=' ')\n",
    "    print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n",
      "8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "4*1=4 4*2=8 4*3=12 4*4=16 \n",
      "3*1=3 3*2=6 3*3=9 \n",
      "2*1=2 2*2=4 \n",
      "1*1=1 \n"
     ]
    }
   ],
   "source": [
    "# Exercise: 9*9乘法表\n",
    "# 调整成为三角形\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81  \n",
      "8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64  \n",
      "7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49  \n",
      "6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36  \n",
      "5*1=5   5*2=10  5*3=15  5*4=20  5*5=25  \n",
      "4*1=4   4*2=8   4*3=12  4*4=16  \n",
      "3*1=3   3*2=6   3*3=9   \n",
      "2*1=2   2*2=4   \n",
      "1*1=1   \n"
     ]
    }
   ],
   "source": [
    "for row in range(9,0,-1):\n",
    "    for col in range(1,row+1):\n",
    "        m = row*col\n",
    "        print('{}*{}={:<2}'.format(row,col,m), end='  ')\n",
    "        #print(str(i)+'*'+str(j)+'='+str(m), end=' ')\n",
    "    print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1  \n",
      "2*1=2  2*2=4  \n",
      "3*1=3  3*2=6  3*3=9  \n",
      "4*1=4  4*2=8  4*3=12 4*4=16 \n",
      "5*1=5  5*2=10 5*3=15 5*4=20 5*5=25 \n",
      "6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 \n",
      "7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 \n",
      "8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 \n",
      "9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 \n"
     ]
    }
   ],
   "source": [
    "i=1\n",
    "while i<=9:\n",
    "    j=1\n",
    "    while j<=i:\n",
    "        foo = i*j\n",
    "        print('{}*{}={:<2}'.format(i,j,foo),end=' ')\n",
    "        j+=1\n",
    "    print(\"\")\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### break, continue, else, pass 的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0;1;2;3;4;5;"
     ]
    }
   ],
   "source": [
    "# break 退出循环   # 找人\n",
    "for i in range(0,10):\n",
    "    if i==6:\n",
    "        break\n",
    "    print(i,end=';')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9;"
     ]
    }
   ],
   "source": [
    "# continue：跳过本次循环，继续接下来的循环   # 符合条件运行，不符合跳过\n",
    "for i in range(0,2):    \n",
    "    if i==2:\n",
    "        continue\n",
    "print(i,end=';')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "not found\n"
     ]
    }
   ],
   "source": [
    "# Exercise: 数据查找\n",
    "# else\n",
    "m=20\n",
    "a=[1,2,3,4]\n",
    "for i in a:\n",
    "    if i==m:\n",
    "        print('found')\n",
    "        break\n",
    "else:\n",
    "    print('not found')\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pass  空语句用于占位\n",
    "\n",
    "for i in a:\n",
    "    pass\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 2, 'b': 1, 'c': 2, 'd': 1}\n"
     ]
    }
   ],
   "source": [
    "# Exercise: count words\n",
    "\n",
    "names=['a','b','c','a','c','d']\n",
    "\n",
    "counts=dict()\n",
    "\n",
    "for name in names:\n",
    "    if name not in counts:\n",
    "        counts[name]=1\n",
    "    else:\n",
    "        counts[name]=counts[name]+1\n",
    "        \n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "counts.get('cd',0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 2, 'b': 1, 'c': 2, 'd': 1}\n"
     ]
    }
   ],
   "source": [
    "## another way!!\n",
    "counts=dict()\n",
    "for name in names:\n",
    "    counts[name]=counts.get(name,0)+1\n",
    "print(counts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
